use base64::{Engine, prelude::BASE64_STANDARD};
use hmac::Mac;

pub fn signature(
    access_key_secret: &str,
    method: &str,
    content_md5: &str,
    content_type: &str,
    date: &str,
    oss_headers: &str,
    resource: &str,
) -> String {
    let sign_str = format!(
        "{}\n{}\n{}\n{}\n{}{}",
        method, content_md5, content_type, date, oss_headers, resource
    );

    signature_str(access_key_secret, &sign_str)
}

pub fn signature_str(access_key_secret: &str, str: &str) -> String {
    let mut mac = hmac::Hmac::<sha1::Sha1>::new_from_slice(access_key_secret.as_bytes()).unwrap();

    mac.update(str.as_bytes());

    BASE64_STANDARD.encode(mac.finalize().into_bytes())
}
